package com.iweb.newa202104.service.impl;

import com.iweb.newa202104.Mapper.ResponseMapper;
import com.iweb.newa202104.dto.ResponseInputDTO;
import com.iweb.newa202104.dto.UserOutputDTO;
import com.iweb.newa202104.entity.ResponseEntity;
import com.iweb.newa202104.enums.Err;
import com.iweb.newa202104.exception.CommonException;
import com.iweb.newa202104.service.ResponseService;
import com.iweb.newa202104.util.CommonUtil;
import com.iweb.newa202104.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * @Author: LGZ
 * @Date: 2021/5/12 18:25
 * @Description:
 */
@Service
public class ResponseServiceImpl implements ResponseService {

    @Autowired
    ResponseMapper responseMapper;

    @Override
    public ResultVO add(HttpSession httpSession, ResponseInputDTO responseInputDTO) {
        ResponseEntity responseEntity = CommonUtil.getMapperFactory().getMapperFacade().map(responseInputDTO, ResponseEntity.class);
        UserOutputDTO userOutputDTO = (UserOutputDTO)httpSession.getAttribute("user");
        responseEntity.setUserId(userOutputDTO.getUserId());
        try {
            responseMapper.addResponse(responseEntity);
            throw new CommonException(Err.SUCCESS);
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @Override
    public ResultVO getAll(int questionId) {
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setQuestionId(questionId);
        try {
            return CommonUtil.ajaxReturn(responseMapper.getAll(responseEntity));
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @Override
    public ResultVO getMyResponseCounts(HttpSession httpSession) {
        try {
            ResponseEntity responseEntity = new ResponseEntity();
            UserOutputDTO userOutputDTO = (UserOutputDTO)httpSession.getAttribute("user");
            responseEntity.setUserId(userOutputDTO.getUserId());
            return CommonUtil.ajaxReturn(responseMapper.getMyResponseCounts(responseEntity));
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @Override
    public ResultVO getMyResponse(HttpSession httpSession) {
        ResponseEntity responseEntity = new ResponseEntity();
        UserOutputDTO userOutputDTO = (UserOutputDTO)httpSession.getAttribute("user");
        responseEntity.setUserId(userOutputDTO.getUserId());
        try {
            return CommonUtil.ajaxReturn(responseMapper.getMyResponse(responseEntity));
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @Override
    public ResultVO delete(HttpSession httpSession, int responseId) {
        ResponseEntity responseEntity = new ResponseEntity();
        UserOutputDTO userOutputDTO = (UserOutputDTO)httpSession.getAttribute("user");
        responseEntity.setUserId(userOutputDTO.getUserId());
        responseEntity.setResponseId(responseId);
        try {
            responseMapper.delete(responseEntity);
            throw new CommonException(Err.SUCCESS);
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @Override
    public ResultVO giveLike(ResponseInputDTO responseInputDTO) {
        ResponseEntity responseEntity = CommonUtil.getMapperFactory().getMapperFacade().map(responseInputDTO, ResponseEntity.class);
        try {
            responseMapper.giveLike(responseEntity);
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
        throw new CommonException(Err.SUCCESS);
    }
}
